﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyAlbum.Services;
using System.Diagnostics;
using MyAlbum.Model;

namespace UnitTests
{
    /// <summary>
    /// Summary description for AlbumPerformance
    /// </summary>
    [TestClass]
    public class AlbumPerformance
    {
        public AlbumPerformance()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _testConfig = new TestConfig();
            albums = new List<MyAlbum.Model.Album>();
            var user = new GenericUser("tal", new string[] { "Admin" });

            var items = Service.GetItems();
            for (int i = 0; i < items.Count; i++)
            {
                Service.DeleteItem(items[i]);
            }
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            foreach (var item in albums)
            {
                Service.DeleteItem(item);
            }
        }
        TestConfig _testConfig;
        //
        #endregion

        private List<MyAlbum.Model.Album> albums;
        MyAlbum.Services.AlbumService Service
        {
            get
            {
                return ObjectFactory.GetInstance<IAlbumService>() as AlbumService;
            }
        }

        private const int Count = 5000;
        [TestMethod]
        public void CreateAlbumTest()
        {
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(50),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            Stopwatch watch = new Stopwatch();
            List<long> data = new List<long>();
            IAlbumService target=ObjectFactory.GetInstance<IAlbumService>();
            for (int i = 0; i < Count; i++)
            {
               
                if (TestConfig._mode == PersistenceModes.Xml)
                {
                    //create new each loop
                    target = ObjectFactory.GetInstance<IAlbumService>();
                }
                watch.Start();
                target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                data.Add(watch.ElapsedMilliseconds);
                watch.Reset();
                this.albums.Add(item);
            }

            Statistics.Save("AddAlbum", data);

        }

        [TestMethod]
        public void UpdateAlbumTest()
        {
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(50),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };

            MyAlbum.Model.Album update = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now
            };
            Stopwatch watch = new Stopwatch();
            List<long> data = new List<long>();
            IAlbumService target = ObjectFactory.GetInstance<IAlbumService>();
            for (int i = 0; i < Count; i++)
            {
                if (TestConfig._mode == PersistenceModes.Xml)
                {
                    //create new each loop
                    target = ObjectFactory.GetInstance<IAlbumService>();
                }
                target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                update.Id = item.Id - 1;
                watch.Start();
                target.UpdateItem(update, new MyAlbum.Utility.GenericValidationSummary());
                data.Add(watch.ElapsedMilliseconds);
                watch.Reset();
                this.albums.Add(item);
            }

            Statistics.Save("UpdateAlbum", data);

        }

        [TestMethod]
        public void GetAlbumsBySectionIDTest()
        {
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(50),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Visible = true
            };

            Stopwatch watch = new Stopwatch();
            List<long> data = new List<long>();
            IAlbumService target = ObjectFactory.GetInstance<IAlbumService>();
            for (int i = 0; i < Count; i++)
            {
                if (TestConfig._mode == PersistenceModes.Xml)
                {
                    //create new each loop
                    target = ObjectFactory.GetInstance<IAlbumService>(); ;
                }
                target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                watch.Start();
                var user = new GenericUser("tal", new string[] { "" });

                var albums = target.GetAlbumsBySectionId(i % 20);
                int j = albums.Count;
                data.Add(watch.ElapsedMilliseconds);
                watch.Reset();
                this.albums.Add(item);
            }

            Statistics.Save("GetAlbumsBySection", data);

        }

        [TestMethod]
        public void GetAlbumsTest()
        {
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(50),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Visible = true
            };

            Stopwatch watch = new Stopwatch();
            List<long> data = new List<long>();
            IAlbumService target = ObjectFactory.GetInstance<IAlbumService>();
            for (int i = 0; i < Count; i++)
            {
                if (TestConfig._mode == PersistenceModes.Xml)
                {
                    //create new each loop
                    target = ObjectFactory.GetInstance<IAlbumService>();
                }
                target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                watch.Start();
                var user = new GenericUser("tal", new string[] { "" });

                var albums = target.GetItems();
                int j = albums.Count;
                data.Add(watch.ElapsedMilliseconds);
                watch.Reset();
                this.albums.Add(item);
            }

            Statistics.Save("GetItems", data);

        }

        [TestMethod]
        public void GetItemByIdTest()
        {
            MyAlbum.Model.Album item = new MyAlbum.Model.Album()
            {
                Description = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString(),
                SectionId = new Random().Next(50),
                Url = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Visible = true
            };

            Stopwatch watch = new Stopwatch();
            List<long> data = new List<long>();
            IAlbumService target = ObjectFactory.GetInstance<IAlbumService>();
            for (int i = 0; i < Count; i++)
            {
                if (TestConfig._mode == PersistenceModes.Xml)
                {
                    //create new each loop
                    target = ObjectFactory.GetInstance<IAlbumService>();
                }
                target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                watch.Start();
                var t = target.GetItemById(item.Id - 1);
                if (t == null)
                {


                }
                else
                {
                    t.Name = "";
                }
                data.Add(watch.ElapsedMilliseconds);
                watch.Reset();
                this.albums.Add(item);
            }

            Statistics.Save("GetItemById", data);

        }
    }
}
